<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>广告账户编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-form" style="padding: 20px 30px 0 0;">

    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
            <input type="radio" name="type" value="1" title="投放号" lay-filter="type" {$info['type'] == 1 ? 'checked' : ''} />
            <input type="radio" name="type" value="2" title="投放媒体" lay-filter="type" {$info['type'] == 2 ? 'checked' : ''} />
            <input type="radio" name="type" value="3" title="推广链接" lay-filter="type" {$info['type'] == 3 ? 'checked' : ''} />
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">消耗金额</label>
        <div class="layui-input-block">
            <input type="text" name="money" value="{$info.money}" lay-verify="required" lay-verType="tips" lay-reqText="消耗金额不能为空" placeholder="请输入消耗金额" autocomplete="off" class="layui-input" />
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">消耗日期</label>
        <div class="layui-input-block">
            <input type="text" id="dayStart" value="{$info.cost_date}" name="cost_date" placeholder="请选择消耗日期" autocomplete="off" class="layui-input">
        </div>
    </div>

    {neq name="role_name" value="channel"}
    <div class="layui-form-item">
        <label class="layui-form-label">关联投放号</label>
        <div class="layui-input-block">
            <select name="channel_id" lay-search lay-verify="channel_id" lay-verType="tips" lay-filter="channel_switch">
                <option value="0">选择投放号</option>
                {foreach $channels as $k => $v}
                <option value="{$v.id}" {$info['channel_id'] == $v['id'] ? 'selected' : ''}>{$v.account}</option>
                {/foreach}
            </select>
        </div>
    </div>
    {/neq}

    <div class="layui-form-item app-block-1" {$info['type'] != 1 ? 'style="display: block"' : 'style="display: none"'}>
        <label class="layui-form-label">投放媒体</label>
        <div class="layui-input-block">
            <select name="advert_media_id" lay-verify="advert_media_id" lay-verType="tips" lay-filter="advert_media_switch">
                <option value="0"></option>
                {foreach $advert_medias as $k => $v}
                <option value="{$k}" {$info['advert_media_id'] == $k ? 'selected' : ''}>{$v}</option>
                {/foreach}
            </select>
        </div>
    </div>

    <div class="layui-form-item app-block-2" {$info['type'] == 3 ? 'style="display: block"' : 'style="display: none"'}>
        <label class="layui-form-label">推广链接</label>
        <div class="layui-input-block">
            <select name="link_id" lay-search lay-verify="link_id" lay-verType="tips">
                <option value="0"></option>
                {foreach $links as $k => $v}
                <option value="{$v.id}" {$info['link_id'] == $v['id'] ? 'selected' : ''}>{$v.name}</option>
                {/foreach}
            </select>
        </div>
    </div>

    <div class="layui-form-item layui-hide">
        <input type="hidden" name="id" value="{$info.id}" />
        <button class="layui-btn" lay-submit lay-filter="LAY-data-edit-submit" id="LAY-data-edit-submit">提交</button>
    </div>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.use('laydate', function () {
        var $ = layui.$;
        var laydate = layui.laydate;

        var nowTime = new Date().valueOf();
        var start = laydate.render({
            elem: '#dayStart',
            type: 'date',
            max: nowTime,
            done: function (value, date) {

            }
        });
    });

    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function(){
        var $ = layui.$
            ,form = layui.form;

        var ctype = 1;
        form.on('radio(type)', function(data){

            ctype = data.value;
            if(data.value == 1)
            {
                $('.app-block-1').hide();
                $('.app-block-2').hide();
            }else if(data.value == 2){
                $('.app-block-1').show();
                $('.app-block-2').hide();
            }else if(data.value == 3){
                $('.app-block-1').show();
                $('.app-block-2').show();
            }
        });

        var channel_id = {$info['channel_id']};
        var advert_media_id = {$info['advert_media_id']};

        form.on('select(channel_switch)', function(data){
            channel_id = data.value;

            $.ajax({
                url:"{:url('Backhaul/channel_link')}",
                type:"post",
                dataType: "json",
                data: {channel_id:data.value,advert_media_id:advert_media_id},
                async:true,
                success: function(rs){

                    var html = '';
                    if(rs['code'] == 1){
                        $.each(rs.data,function (k,v) {
                            html += '<option value="'+v.id+'">'+v.name+'</option>';
                        })
                    }

                    $('select[name="link_id"]').html(html);
                    form.render();
                    return false;
                }
            });
        });

        form.on('select(advert_media_switch)', function(data){
            advert_media_id = data.value;

            $.ajax({
                url:"{:url('Backhaul/channel_link')}",
                type:"post",
                dataType: "json",
                data: {advert_media_id:data.value,channel_id:channel_id},
                async:true,
                success: function(rs){

                    var html = '';
                    if(rs['code'] == 1){
                        $.each(rs.data,function (k,v) {
                            html += '<option value="'+v.id+'">'+v.name+'</option>';
                        })
                    }

                    $('select[name="link_id"]').html(html);
                    form.render();
                    return false;
                }
            });
        });

        form.verify({

            channel_id: function (value) {
                if(value < 1){
                    return '请选择关联投放号';
                }
            }

        });

    })

</script>
</body>
</html>